import request from '@/utils/request'

// 登录请求参数
export interface LoginRequest {
  username: string
  password: string
}

// 用户信息
export interface UserInfo {
  id: number
  username: string
  name: string
  email: string
  roles: Array<{
    id: number
    name: string
  }>
  permissions: Array<{
    id: number
    name: string
    code: string
    description: string
  }>
  menus: MenuTree[]
}

// 菜单树结构
export interface MenuTree {
  id: number
  parent_id?: number
  name: string
  path: string
  component: string
  redirect?: string
  meta?: {
    title?: string
    icon?: string
    affix?: boolean
    hidden?: boolean
  }
  sort: number
  hidden: boolean
  children?: MenuTree[]
}

// 登录响应
export interface LoginResponse {
  token: string
  user: UserInfo
}

// 修改密码请求
export interface ChangePasswordRequest {
  old_password: string
  new_password: string
}

/**
 * 用户登录
 */
export function login(data: LoginRequest): Promise<LoginResponse> {
  return request.post('/auth/login', data)
}

/**
 * 获取用户信息
 */
export function getUserInfo(): Promise<UserInfo> {
  return request.get('/auth/info')
}

/**
 * 获取用户菜单
 */
export function getUserMenus(): Promise<{ menus: MenuTree[] }> {
  return request.get('/auth/menus')
}

/**
 * 用户登出
 */
export function logout(): Promise<{ message: string }> {
  return request.post('/auth/logout')
}

/**
 * 刷新令牌
 */
export function refreshToken(): Promise<{ token: string }> {
  return request.post('/auth/refresh')
}

/**
 * 修改密码
 */
export function changePassword(data: ChangePasswordRequest): Promise<{ message: string }> {
  return request.post('/auth/change-password', data)
}
